package com.ziq.baselib.test.horizontalpage

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.ziq.base.base.BaseActivity
import com.ziq.base.extension.binding
import com.ziq.base.extension.click
import com.ziq.base.view.recycleview.BaseMultiViewHolder
import com.ziq.baselib.R
import com.ziq.baselib.databinding.ActivityHorizontalPageBinding
import dagger.hilt.android.AndroidEntryPoint
import java.io.*


@AndroidEntryPoint
class HorizontalPageActivity2 : BaseActivity() {

    private val binding : ActivityHorizontalPageBinding by binding()

    val adapter = MyAdapter()
    val dataList = mutableListOf<String>()

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        for (index in 0..13){
            dataList.add("a$index")
        }


        binding.root
        binding.recyclerView.layoutManager = HorizontalPageLayoutManager(2, 4)
//        binding.recyclerView.layoutManager = GridLayoutManager(this, 4, RecyclerView.HORIZONTAL, false)
        binding.recyclerView.adapter = adapter
//        binding.recyclerView.itemAnimator = null

        val pagingScrollHelper = PagingScrollHelper()
        pagingScrollHelper.setUpRecycleView(binding.recyclerView)
        pagingScrollHelper.setOnPageChangeListener {  }
        binding.btnUpdate.click {
            adapter.notifyDataSetChanged()
        }

        binding.btnRemove.click {
            dataList.removeAt(1)
            adapter.notifyItemRemoved(1)
        }
    }

   inner class MyAdapter : RecyclerView.Adapter<BaseMultiViewHolder>(){

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseMultiViewHolder {
            val view: View = LayoutInflater.from(parent.context).inflate(R.layout.item_text, parent, false)
            return BaseMultiViewHolder(view)
        }

        override fun onBindViewHolder(holder: BaseMultiViewHolder, position: Int) {
            val item = dataList.get(position)
            holder.getViewById<TextView>(R.id.tv).text = "$position : $item"
        }

        override fun getItemCount(): Int {
            return dataList.size
        }

    }
}